/*
 * 版权所有 (c) 华为技术有限公司 2022-2022
 */
package com.huawei.cameratest;

import android.app.Application;
import android.graphics.Rect;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.util.Log;
import android.util.Range;

import static android.media.MediaCodecList.ALL_CODECS;

public class MyApplication extends Application {
    public static int Max_width = 0;
    public static int Max_height = 0;
    public static Range<Integer> HRange264;
    public static Range<Integer> WRange264;
    public static Range<Integer> HRange265;
    public static Range<Integer> WRange265;
    public static final int VER_ROW = 4;
    public static Rect saveRect;
    public static int startTime = 0;

    String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        search();
        videoMaxSize();
    }

    // sample_pdm 6 1 2 0 0 0
    private void search() {
        MediaCodecList mediaCodecList = new MediaCodecList(ALL_CODECS);
        MediaCodecInfo[] mediaCodecInfos = mediaCodecList.getCodecInfos();
        for (MediaCodecInfo me : mediaCodecInfos) {
            for (String type : me.getSupportedTypes()) {
                MediaCodecInfo.CodecCapabilities mediaCodecInfo = me.getCapabilitiesForType(type);
                MediaCodecInfo.VideoCapabilities videoCapabilities = mediaCodecInfo.getVideoCapabilities();
                if (videoCapabilities != null) {
                    int hs = videoCapabilities.getSupportedHeights().getUpper();
                    int ws = videoCapabilities.getSupportedWidths().getUpper();
                    Log.i(TAG, "search: w * h" + hs + "*" + ws);
                    if (hs * ws > Max_height * Max_width) {
                        Max_height = hs;
                        Max_width = ws;
                    }
                }
            }
        }
    }

    private void videoMaxSize() {
        MediaCodecInfo[] mediaCodecInfos = new MediaCodecList(ALL_CODECS).getCodecInfos();
        for (MediaCodecInfo mediaCodecInfo : mediaCodecInfos) {
            if (mediaCodecInfo.isEncoder()) {
                String[] types = mediaCodecInfo.getSupportedTypes();
                for (String type : types) {
                    if (type.equals("video/avc")) {
                        MediaCodecInfo.VideoCapabilities videoCapabilitie = mediaCodecInfo.getCapabilitiesForType(type).getVideoCapabilities();
                        if (WRange264 == null) {
                            WRange264 = videoCapabilitie.getSupportedWidths();
                        } else {
                            WRange264 = WRange264.extend(videoCapabilitie.getSupportedWidths());
                        }

                        if (HRange264 == null) {
                            HRange264 = videoCapabilitie.getSupportedHeights();
                        } else {
                            HRange264 = HRange264.extend(videoCapabilitie.getSupportedHeights());
                        }
                    } else if (type.equals("video/hevc")) {
                        MediaCodecInfo.VideoCapabilities videoCapabilitie = mediaCodecInfo.getCapabilitiesForType(type).getVideoCapabilities();
                        if (WRange265 == null) {
                            WRange265 = videoCapabilitie.getSupportedWidths();
                        } else {
                            WRange265 = WRange265.extend(videoCapabilitie.getSupportedWidths());
                        }

                        if (HRange265 == null) {
                            HRange265 = videoCapabilitie.getSupportedHeights();
                        } else {
                            HRange265 = HRange265.extend(videoCapabilitie.getSupportedHeights());
                        }
                    }
                }
            }
        }
    }

    public static Range<Integer> getHRange264() {
        return HRange264;
    }

    public static Range<Integer> getWRange264() {
        return WRange264;
    }

    public static int getMaxWidth() {
        return Max_width;
    }

    public static int getMaxHeight() {
        return Max_height;
    }

}
